package com.yanqu.road.dao.impl.activity.feast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.feast.data.CrossFeastMountAllotData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossFeastMountAllotDataDaoImpl extends TempDataObjectDao<CrossFeastMountAllotData> {

    public static String getCrossFeastMountAllotDataSql = "select * from `t_c_feast_mount_allot_data` where `activityId`=?";
    public Map<Long, CrossFeastMountAllotData> getCrossFeastMountAllotData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossFeastMountAllotData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossFeastMountAllotDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossFeastMountAllotData data = parse(rs);
                map.put(data.getAllotTimeId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossFeastMountAllotDataSql = "update `t_c_feast_mount_allot_data` set ackServerIdList=? where activityId=? and allotTimeId=?";
    @Override
    protected boolean updateData(CrossFeastMountAllotData crossFeastMountAllotData) {
        crossFeastMountAllotData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFeastMountAllotData.getAckServerIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossFeastMountAllotData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFeastMountAllotData.getAllotTimeId()));
        if (getDb().executeNoneQuery(updateCrossFeastMountAllotDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossFeastMountAllotDataSql = "insert into `t_c_feast_mount_allot_data`(activityId,allotTimeId,ackServerIdList) values (?,?,?)";
    @Override
    protected boolean addData(CrossFeastMountAllotData crossFeastMountAllotData) {
        crossFeastMountAllotData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossFeastMountAllotData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFeastMountAllotData.getAllotTimeId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFeastMountAllotData.getAckServerIdList())));
        if (getDb().executeNoneQuery(insertCrossFeastMountAllotDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossFeastMountAllotData parseData(ResultSet rs) throws SQLException {
        CrossFeastMountAllotData crossFeastMountAllotData = new CrossFeastMountAllotData();
        crossFeastMountAllotData.setActivityId(rs.getInt("activityId"));
        crossFeastMountAllotData.setAllotTimeId(rs.getLong("allotTimeId"));
        crossFeastMountAllotData.setAckServerIdList(JSON.parseObject(rs.getString("ackServerIdList"), new TypeReference<List<Long>>() {}));

        return crossFeastMountAllotData;
    }
}
